package com.heu.blood.storge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.storge.dao.BloodInventoryOpsDao;
import com.heu.blood.storge.entity.BloodInventoryOpsEntity;
import com.heu.blood.storge.service.BloodInventoryOpsService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@Service("bloodInventoryOpsService")
public class BloodInventoryServiceImpl extends ServiceImpl<BloodInventoryOpsDao, BloodInventoryOpsEntity> implements BloodInventoryOpsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = (String)params.get("startTime");
        String endTime = (String)params.get("endTime");
        Date startTimeQuery = null;
        Date endTimeQuery = null;
        if(Strings.isNotBlank(startTime)){
            try {
                startTimeQuery = sdf.parse(startTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(Strings.isNotBlank(endTime)){
            try {
                endTimeQuery = sdf.parse(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        LambdaQueryWrapper<BloodInventoryOpsEntity> queryWrapper = new LambdaQueryWrapper<>();
        if(startTimeQuery != null){
            queryWrapper.ge(BloodInventoryOpsEntity::getGmtCreate,startTimeQuery);
        }
        if(endTimeQuery != null){
            queryWrapper.le(BloodInventoryOpsEntity::getGmtCreate,endTimeQuery);
        }
        queryWrapper.orderByDesc(BloodInventoryOpsEntity::getGmtCreate);
        IPage<BloodInventoryOpsEntity> page = this.page(
                new Query<BloodInventoryOpsEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}